package Jets.ui
{
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.display.DisplayObjectContainer;
   import flash.events.MouseEvent;
   import flash.utils.getTimer;
   
   import Jets.display.JContainer;
   import Jets.ui.inface.IIcon;
   import Jets.ui.inface.Ilistable;
   import Jets.ui.skins.SkinStates;
   
   public class JPushButton extends JContainer implements Ilistable, IIcon
   {
	   public var _label:JLabel;
	   private var _labelText:String = "";
	   private var _over:Boolean = false;
	   private var _down:Boolean = false;
	   
      public function JPushButton(parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0, label:String="", click:Function=null) 
	  {
         addEventListener(MouseEvent.CLICK,this.doclick,false,int.MAX_VALUE);
         super(parent,cx,cy);
         if(click != null)
         {
            addEventListener(MouseEvent.CLICK,click);
         }
         this.data = label;
      }
      
	  /**间隔时间，防止快速点击*/
	  private var _InvalidTime:Number = 200;
      public function get InvalidTime() : uint 
	  {
         return this._InvalidTime;
      }
      public function set InvalidTime(value:uint) : void 
	  {
         if(this._InvalidTime != value)
         {
            this._InvalidTime = value;
         }
      }
      
      /**点击按钮后会判断是否能够点击， 点击过快将不会重复发送点击事件*/
      private var __canClickTime:int = 0;
      private function doclick(event:MouseEvent) : void 
	  {
         if(getTimer() > this.__canClickTime)
         {
            this.__canClickTime = getTimer() + this._InvalidTime;
         }
         else
         {
			 event.stopImmediatePropagation();
         }
      }
	  
	  protected var bitmap:Bitmap;
      public function get myIcon() : Bitmap 
	  {
         return this.bitmap;
      }
	  public function set bmpd(bit:BitmapData) : void 
	  {
		  this.bitmap.bitmapData = bit;
		  if(bit)
		  {
			  this.bitmap.x = (this.width - this.bitmap.width) / 2;
			  this.bitmap.y = (this.height - this.bitmap.height) / 2;
			  this.addChild(this.bitmap);
		  }
	  }
	  
      /**传入icon的路径，然后进行加载，此方法后期再弄*/
      protected var _icon:String;
      public function get icon() : String 
	  {
         return this._icon;
      }
      public function set icon(url:String) : void 
	  {
		 /*
         if(url)
         {
            this._icon = url;
            this.bitmap.bitmapData = null;
            if(this.contains(this.bitmap))
            {
               this.removeChild(this.bitmap);
            }
            IconManager.getInstance().req(this as IIcon,url);
         }*/
      }
      
      override protected function init() : void 
	  {
         super.init();
         buttonMode = true;
         this.hasState = true;
         useHandCursor = true;
         this.setSize(100,23);
      }
      
      override protected function addChildren() : void 
	  {
         this.bitmap = new Bitmap();
         this._label = new JLabel();
         this._label.mouseEnabled = false;
         this._label.autoSize = true;
         this._label.usehtml = true;
         addChild(this._label);
         addEventListener(MouseEvent.MOUSE_DOWN,this.onMouseDown);
         addEventListener(MouseEvent.ROLL_OVER,this.onMouseOver);
      }
      
	  /**x方向的位移偏移*/
      private var _offx:int = 0;
      public function set offx(value:int) : void 
	  {
         if(this._offx != value)
         {
            this._offx = value;
            invalidate();
         }
      }
      public function get offx() : int 
	  {
         return this._offx;
      }
      
	  
      override public function draw() : void 
	  {
         super.draw();
         this._label.text = this._labelText;
         var cwidth:int = _width - this._offx;
         this._label.move(cwidth / 2 - this._label.width / 2,_height / 2 - this._label.height / 2);
         this.bitmap.x = (this.width - this.bitmap.width) / 2;
         this.bitmap.y = (this.height - this.bitmap.height) / 2;
      }
      
      protected function onMouseOver(param1:MouseEvent) : void 
	  {
         this._over = true;
         state = SkinStates.OVER;
         addEventListener(MouseEvent.ROLL_OUT,this.onMouseOut);
      }
      
      protected function onMouseOut(param1:MouseEvent) : void 
	  {
         this._over = false;
         if(!this._down)
         {
            state = SkinStates.UP;
         }
      }
      
      protected function onMouseDown(param1:MouseEvent) : void 
	  {
         this._down = true;
         state = SkinStates.DOWN;
         stage.addEventListener(MouseEvent.MOUSE_UP,this.onMouseUp);
      }
      
      protected function onMouseUp(param1:MouseEvent) : void 
	  {
         this._down = false;
         state = SkinStates.UP;
         if(stage)
         {
            stage.removeEventListener(MouseEvent.MOUSE_UP,this.onMouseUp);
         }
      }
      
      override public function setSize(cw:Number, ch:Number) : void 
	  {
         super.setSize(cw,ch);
      }
      
	  /**
	  * 文本颜色
	  */ 
      public function set textColor(color:uint) : void 
	  {
         this._label.textColor = color;
      }
      public function get textColor() : uint 
	  {
         return this._label.textColor;
      }
      
	  
	  public function set usehtml(bool:Boolean):void
	  {
		  this._label.usehtml = bool;
	  }
	  public function get usehtml():Boolean
	  {
		  return this._label.usehtml;
	  }
	  
	  /**
	  * 文本内容
	  * 可以传入字符串，数值 以及带有text(String,number)属性的对象
	  */ 
      override public function set data(value:Object) : void 
	  {
         if(value == null)
         {
            this._labelText = "";
         }
         else
         {
            if(value is Number || value is String)
            {
               this._labelText = value + "";
            }
            else
            {
               this._labelText = value.text;
            }
         }
         invalidateNow();
      }
      override public function get data() : Object 
	  {
         return this._labelText;
      }
	  
   }
}
